package classloader;

import java.lang.reflect.Method;


public class ClassIdentity {

	public static void main(String[] args) {
//		new ClassIdentity().testClassIdentity();
		testInterface();
	}
	
	public void testClassIdentity() {
		String classDataRootPath = "E:\\tmp\\classloader";
		FileSystemClassLoader fscl1 = new FileSystemClassLoader(classDataRootPath);
		FileSystemClassLoader fscl2 = new FileSystemClassLoader(classDataRootPath);
		String className = "test.Sample";
		try {
			Class<?> class1 = fscl1.loadClass(className);
			Object obj1 = class1.newInstance();
			Class<?> class2 = fscl2.loadClass(className);
			Object obj2 = class2.newInstance();
			Method setSampleMethod = class1.getMethod("setSample", java.lang.Object.class);
			setSampleMethod.invoke(obj1, obj2);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}
	
	public static void testInterface(){
		String classDataRootPath = "E:\\tmp\\classloader";
		FileSystemClassLoader fscl1 = new FileSystemClassLoader(classDataRootPath);
//		FileSystemClassLoader fscl2 = new FileSystemClassLoader(classDataRootPath);
		String basicClassName = "test.CalculatorBasic";
		String advancedClassName = "test.CalculatorAdvanced";
		try {
			Method method = ICalculator.class.getDeclaredMethod("calculate",String.class);
			Class<?> clazz = fscl1.loadClass(basicClassName);
			ICalculator calculator = (ICalculator) clazz.newInstance();
			System.out.println(calculator.getVersion());
			clazz = fscl1.loadClass(advancedClassName);
			calculator = (ICalculator) clazz.newInstance();
			System.out.println(calculator.getVersion());
			System.out.println(method.invoke(calculator,"www"));
			System.out.println(calculator instanceof ICalculator);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
